package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.customView.ConfirmDialog;
import com.dlc.felear.xiaoensale.entity.MenuRecordInfo;

import java.util.ArrayList;
import java.util.List;


public class MenuRecordltemAdapter extends BaseAdapter implements ConfirmDialog.ConfirmDialogCallback{

    private static final String TAG = "MenuRecordltemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<MenuRecordInfo> items;
    private ConfirmDialog confirmDialog;
    private int selectIndex=0;

    public MenuRecordltemAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
        confirmDialog=new ConfirmDialog(context,this);
    }

    public List<MenuRecordInfo> getData() {
        return items;
    }


    public void setItems(List<MenuRecordInfo> data) {
        if (data==null||data.isEmpty()) {
            notifyDataSetChanged();
            return;
        }
        items.clear();
        items.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public MenuRecordInfo getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_menu_record_item, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_date = convertView.findViewById(R.id.tv_date);
            holder.tv_gg = convertView.findViewById(R.id.tv_gg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String name=items.get(position).getName();
        holder.tv_name.setText(name);
        holder.tv_date.setText(items.get(position).getDate());
        holder.tv_gg.setText(items.get(position).getSpecification()+"斤");
        
        return convertView;
    }

    private void delete(int position){
        selectIndex=position;
        confirmDialog.showDialog("确定删除该菜谱",1);
    }
    private void upload(int position){
        selectIndex=position;
        confirmDialog.showDialog("确定上传该菜谱",2);
    }

    @Override
    public void selectResult(int value,int id) {
        if(value==-1){
            selectIndex=-1;
            return;
        }
        if(id==1){
            //删除
            if(selectIndex!=-1){
                deleteItem(items.get(selectIndex));
                items.remove(selectIndex);
                notifyDataSetChanged();
            }else {
                toastMsg("未选中上传的菜谱");
            }
        }
        if(id==2){
            //上传
            if(selectIndex!=-1){
            }else {
                toastMsg("未选中上传的菜谱");
            }
        }
    }

    private void deleteItem(MenuRecordInfo MenuRecordInfo){
        try {

        }catch (Exception e){
            Log.e(TAG,e.getMessage());
        }

    }



    static class ViewHolder {
        TextView tv_name,tv_date,tv_gg;
    }

    private void toastMsg(String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT);
    }

}